package com.tsx.leetcode.hot100.easy;

import java.util.Arrays;

/**
 * <a href="https://leetcode.cn/problems/counting-bits">比特位计数</a>
 */
public class Q338CountingBits {
    public static void main(String[] args) {
        int[] ints = countBits(5);
        System.out.println("Arrays.toString(ints) = " + Arrays.toString(ints));
    }

    public static int[] countBits(int n) {
        int[] res = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            res[i] = Integer.bitCount(i);
        }
        return res;
    }

    public static int[] countBitsByOwner(int n) {
        int[] nums = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            nums[i] = countOneNums(i);
        }
        return nums;
    }

    public static int countOneNums(int n) {
        int cnt = 0;
        while (n != 0) {
            cnt += (n & 1);
            n = n >> 1;
        }
        return cnt;
    }
}
